#!/bin/sh
# - nr_processes
# - ip_addr
# - n_recordcount
# - n_operationcount
# - is_readallfields
# - n_readproportion
# - n_updateproportion
# - n_scanproportion
# - n_insertproportion
# - c_requestdistribution
# - ycsb_cpu_node_bind
# - ycsb_mem_node_bind
# - nr_threads
# - preload

##The Yahoo! Cloud Serving Benchmark (YCSB) is an open-source
##specification and program suite for evaluating retrieval and
##maintenance capabilities of computer programs. It is often
##used to compare relative performance of NoSQL database
##management systems.

. "$LKP_SRC"/lib/reproduce-log.sh
. "$LKP_SRC"/lib/numactl.sh
. $LKP_SRC/lib/tests/cassandra.sh

which python3 > /dev/null && PYTHON3_PATH=$(which python3)
ln -s $PYTHON3_PATH /usr/bin/python

setup_java_home

export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
export PATH=$BENCHMARK_ROOT/ycsb/ycsb/bin:$PATH

: "${nr_processes:=1}"

proc_n_recordcount=$((n_recordcount/nr_processes))
proc_n_operationcount=$((n_operationcount/nr_processes))

for i in $(seq 1 "$nr_processes")
do
        port=$((27016 + i))
        cat > "$TMP_RESULT_ROOT/configfile_mongodb.$i" <<EOF
mongodb.url=mongodb://${ip_addr}:$port
mongodb.database=ycsbdb
mongodb.writeConcern=none
EOF
done

if [ -n "$preload" ]; then
        cat > "$TMP_RESULT_ROOT"/workload_mongodb_preload <<EOF
recordcount=${proc_n_recordcount}
operationcount=${proc_n_operationcount}
workload=com.yahoo.ycsb.workloads.CoreWorkload

readallfields=${is_readallfields}

readproportion=0
updateproportion=1
scanproportion=${n_scanproportion}
insertproportion=${n_insertproportion}

requestdistribution=uniform
EOF
fi

cat > "$TMP_RESULT_ROOT"/workload_mongodb <<EOF
recordcount=${proc_n_recordcount}
operationcount=${proc_n_operationcount}
workload=com.yahoo.ycsb.workloads.CoreWorkload

readallfields=${is_readallfields}

readproportion=${n_readproportion}
updateproportion=${n_updateproportion}
scanproportion=${n_scanproportion}
insertproportion=${n_insertproportion}

requestdistribution=${c_requestdistribution}
EOF

ycsb_dir="$TMP"/ycsb/
rm -rf "$ycsb_dir"
mkdir -p "$ycsb_dir"

parse_numa_node_binding "$ycsb_cpu_node_bind" "$ycsb_mem_node_bind"

if [ -n "$preload" ]; then
        begin=$(date +%s.%N)
        for i in $(seq 1 "$nr_processes")
        do
                args="\
                load mongodb \
                -P $TMP_RESULT_ROOT/workload_mongodb_preload \
                -P $TMP_RESULT_ROOT/configfile_mongodb.$i \
                -s \
                -threads $nr_threads \
                "
                numa_bind=$(numa_node_binding "$i")
                log_test $numa_bind ycsb $args > /dev/null &
        done
        wait
        end=$(date +%s.%N)
        duration=$(echo "$end-$begin" |bc)
        echo "preload duration: $duration"
fi

for i in $(seq 1 "$nr_processes")
do
        args="\
        run mongodb \
        -P $TMP_RESULT_ROOT/workload_mongodb \
        -P $TMP_RESULT_ROOT/configfile_mongodb.$i \
        -s \
        -threads $nr_threads \
        "
        numa_bind=$(numa_node_binding "$i")
        log_test $numa_bind ycsb $args > "$ycsb_dir"/ycsb.$i &
done

wait

cat "$ycsb_dir"/ycsb.*
